package com.csun.cmny.provider.controller;

import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.OmcExpenseType;
import com.csun.cmny.provider.model.domain.OmcOldManTypeIetype;
import com.csun.cmny.provider.model.domain.OmcUserType;
import com.csun.cmny.provider.model.dto.OmcOldManIetypeDto;
import com.csun.cmny.provider.model.dto.OmcOldManTypeDto;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcExpenseTypeService;
import com.csun.cmny.provider.service.OmcOldManTypeIetypeService;
import com.csun.cmny.provider.service.OmcUserTypeService;
import com.csun.cmny.provider.vo.OmcOldManTypeIetypeVo;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author chenzheng
 * @description:人员类别减免控制器
 * @date 2019/8/12 15:00
 */
@RestController
@RequestMapping(value = "/expenseMiti", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcOldManTypeIetypeController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @Resource
    private OmcOldManTypeIetypeService omcOldManTypeIetypeService;

    @Resource
    private OmcUserTypeService omcUserTypeService;

    @Resource
    private OmcExpenseTypeService expenseTypeService;


    /**
     * 人员类别减免或者修改
     *
     * @param omcOldManIetypeDto
     * @param br
     * @return
     */
    @RequestMapping(value = "/addAndUpdate", method = RequestMethod.POST)
    public Wrapper addAndUpdate(@RequestBody OmcOldManIetypeDto omcOldManIetypeDto, BindingResult br) {
        Long groupId = getLoginAuthDto().getGroupId();
        if (null != omcOldManIetypeDto.getGroupId() && omcOldManIetypeDto.getGroupId() > 0) {
            //传机构ID,就用传过来的机构ID
            groupId = omcOldManIetypeDto.getGroupId();
        }
        List<OmcOldManTypeDto> oldManTypeIetypeList = omcOldManIetypeDto.getOldManTypeIetypeList();
        //参数检验
        for (OmcOldManTypeDto omcOldManTypeIetypeDto : oldManTypeIetypeList) {
            if (null == omcOldManTypeIetypeDto.getId() || 0 == omcOldManTypeIetypeDto.getId()) {//新增
                //验证费用类别ID和人员类别ID不能为空
                if (null == omcOldManTypeIetypeDto.getUserTypeId() || 0 == omcOldManTypeIetypeDto.getUserTypeId()) {
                    return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("old.man.type.ietype.user.type.id.is.not.null", null), omcOldManTypeIetypeDto);
                }
                if (null == omcOldManTypeIetypeDto.getItemId() || 0 == omcOldManTypeIetypeDto.getItemId()) {
                    return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("old.man.type.ietype.item.id.is.not.null", null), omcOldManTypeIetypeDto);
                }
                OmcUserType omcUserTypeDto = new OmcUserType();
                omcUserTypeDto.setGroupId(groupId);
                omcUserTypeDto.setId(omcOldManTypeIetypeDto.getUserTypeId());
                OmcUserType omcUserType = omcUserTypeService.selectOne(omcUserTypeDto);
                if (null == omcUserType) {
                    return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("old.man.type.ietype.user.type.is.not.exist", null), omcOldManTypeIetypeDto);
                }
                OmcExpenseType omcExpenseTypeDto = new OmcExpenseType();
                omcExpenseTypeDto.setGroupId(groupId);
                omcExpenseTypeDto.setId(omcOldManTypeIetypeDto.getItemId());
                OmcExpenseType omcExpenseType = expenseTypeService.selectOne(omcExpenseTypeDto);
                if (null == omcExpenseType) {
                    return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("old.man.type.ietype.item.is.not.exist", null), omcOldManTypeIetypeDto);
                }
                //新增时,费用模块ID和费用类别ID不能存在绑定关系
                OmcOldManTypeIetype omcOldManTypeIetype = new OmcOldManTypeIetype();
                omcOldManTypeIetype.setGroupId(groupId);
                omcOldManTypeIetype.setUserTypeId(omcOldManTypeIetypeDto.getUserTypeId());
                omcOldManTypeIetype.setItemId(omcOldManTypeIetypeDto.getItemId());
                int count = omcOldManTypeIetypeService.selectCount(omcOldManTypeIetype);
                if (count > 0) {
                    return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("old.man.type.ietype.exist.error", null), omcOldManTypeIetypeDto);
                }
            }
            if (null == omcOldManTypeIetypeDto.getPrecentDouble() || omcOldManTypeIetypeDto.getPrecentDouble() < 0 || omcOldManTypeIetypeDto.getPrecentDouble() > 1) {
                return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("old.man.type.ietype.precent.error", null), omcOldManTypeIetypeDto);
            }
            //填充机构数据及处理减免百分比数据
            omcOldManTypeIetypeDto.setGroupId(groupId);
            double precent = omcOldManTypeIetypeDto.getPrecentDouble().doubleValue() * 100;
            omcOldManTypeIetypeDto.setPrecent(Integer.parseInt(new java.text.DecimalFormat("0").format(precent)));//进行四舍五入转化为Integer
        }
        //处理入参,将其转化为OmcOldManTypeIetype类
        List<OmcOldManTypeIetype> list = new ArrayList<>();
        for (OmcOldManTypeDto omcOldManTypeDto : oldManTypeIetypeList) {
            OmcOldManTypeIetype omcOldManTypeIetype = new OmcOldManTypeIetype();
            BeanUtils.copyProperties(omcOldManTypeDto, omcOldManTypeIetype);
            list.add(omcOldManTypeIetype);
        }
        int n = omcOldManTypeIetypeService.batchSaveOmcOldManTypeIetype(list, getLoginAuthDto());
        if (n <= 0) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("operation.failed", null), n);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), n);
    }

    /**
     * 人员类别减免列表查询
     *
     * @return
     */
    @RequestMapping(value = "/showAllExpenseMiti", method = RequestMethod.POST)
    public Wrapper showAllExpenseMiti(Long groupId) {
        OmcUserType omcUserType = new OmcUserType();
        if (null != groupId && groupId > 0) {
            omcUserType.setGroupId(groupId);
        }else{
            //没传机构ID,存储登录用户的机构ID
            omcUserType.setGroupId(getLoginAuthDto().getGroupId());
        }
        List<OmcUserType> omcUserTypes = omcUserTypeService.select(omcUserType);
        List<OmcOldManTypeIetypeVo> list = omcOldManTypeIetypeService.queryOldManTypeIetypeList(omcUserTypes);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("comment.query.success", null), list);
    }

}
